﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ThreadInvoke
{
    public partial class Form1 : Form
    {
        System.Timers.Timer timer = new System.Timers.Timer();
        public Form1()
        {
            InitializeComponent();
        }



        private void btnThreadVisit1_Click(object sender, EventArgs e)
        {
            VisitControls("200");

            Thread th = new Thread(() =>
            {

                //this.lblValue1.Invoke(new Action<string>(data =>
                //{
                //    this.lblValue1.Text = data;
                //    Thread.Sleep(2000);
                //}), "100");

                //this.lblValue1.BeginInvoke(new Action<string>(data =>
                //{
                //    this.lblValue1.Text = data;
                //    Thread.Sleep(5000);
                //}), "100");
                VisitControls("100");

                Console.WriteLine("***********1************");
                Console.WriteLine("***********2************");
            });
            th.IsBackground = true;
            th.Start();
        }

        private void VisitControls(string datas)
        {
            if(this.lblValue1.InvokeRequired)
            {
                this.lblValue1.BeginInvoke(new Action<string>(data =>
                {
                    this.lblValue1.Text = data;
                    Thread.Sleep(5000);
                }), datas);
            }
            else
            {
                this.lblValue1.Text = datas;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer.Interval = 2000;
            timer.Elapsed += Timer_Elapsed;
        }


        int i = 0;
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Console.WriteLine("************" + i++.ToString());
            for (int i = 0; i < 100000; i++)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("5秒");
        }

        private void btnSysTimer_Click(object sender, EventArgs e)
        {
            timer.Start();
        }

        private void btnThreadPool_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem((str)=>{
                Console.WriteLine(str);
            }, "10010");

            ThreadPool.QueueUserWorkItem(DoWork,"10086");
        }

        private void DoWork(object state)
        {
            Console.WriteLine(state);
        }

        private delegate int myDelegate(int a,int b);
        /// <summary>
        /// 对委托进行使用
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public int myFunc(int a,int b)
        {
            return a + b;
        }
        public static bool myFunc2(int a, int b)
        {
            return a == b;
        }

        static void TestDelegate()
        {
            Form1 form1 = new Form1();
            myDelegate fun0 = form1.myFunc;

            // 用匿名函数给委托赋值
            myDelegate fun1 = delegate (int a, int b) { return a + b; };

            //匿名函数 lamda表达式[=>]箭头函数
            myDelegate fun11 = (a, b) => a + b;
            myDelegate fun2 = (a, b) => { return a + b; };

            Func<int,int,bool> fun3 = (int x, int y)=>{ return x == y; };
            Func<int,int,bool> fun4 = (x,y)=>{ return x == y; };
            Func<int,int,bool> fun5 = (x, y)=> x == y;
            Func<int,int,bool> fun6 = new Func<int, int, bool>(myFunc2);



            Console.WriteLine(fun0(1,1));
            Console.WriteLine(fun1(2,2));

        }
    }
}
